/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mobilottery;

import Mobilottery.Constants.Consts.Screens;
import Mobilottery.Interfaces.IAlertCommandListener;
import Mobilottery.Interfaces.IBackgroundOperation;
import Mobilottery.UI.Forms.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

/**
 *
 * @author WindowsVista
 */
public class AppManager
{

    private static Display _display;
    private static Displayable _currentScreen;
    private static Alert _alert;
    private static Alert _loadingAlert;
    private static MIDlet _MIDlet;
    private static boolean _loadingScreenRunning;
    private static int _loadingScreenInterval;
    //public static StringItem MainTitle = new StringItem(null, "");
    //private static boolean _isTickerRunning = false;
    public static String Username = null;
    public static String Password = null;

    public static Display get_display()
    {
        return _display;
    }
    //public static String Password = null;

    public static void startApplication(MIDlet p_MIDlet)
    {
        /*
        new Thread(new Runnable()
        {
        
        public void run()
        {
        _isTickerRunning = true;
        while (_isTickerRunning == true)
        {
        MainTitle.setText(new Date().toString());
        try
        {
        Thread.sleep(1000);
        }
        catch (InterruptedException ex)
        {
        ex.printStackTrace();
        }
        }
        }
        }).start();
         * */

        if (_display == null)
        {
            _MIDlet = p_MIDlet;
            _display = Display.getDisplay(_MIDlet);

            //goToScreen(Screens.MainMenu, null);
            goToScreen(Screens.Login, null);
        }
    }

    static void pauseApplication(MainMidlet aThis)
    {
        //_isTickerRunning = false;
    }

    public static void closeApplication()
    {
        //_isTickerRunning = false;
        _MIDlet.notifyDestroyed();
    }

    public static void setAlert(String p_title, String p_alertText, AlertType p_alertType, final IAlertCommandListener p_alertCommandListener)
    {
        setAlert(p_title, p_alertText, p_alertType, "OK", "Cancel", p_alertCommandListener);
    }

    public static void setAlert(String p_title, String p_alertText, AlertType p_alertType, String p_okCmdName, String p_cancelCmdName, final IAlertCommandListener p_alertCommandListener)
    {
        // rebuild new Alert obj
        _alert = null;
        _alert = new Alert(p_title, p_alertText, null, p_alertType);


        if (p_alertCommandListener != null)
        {
            _alert.setTimeout(Alert.FOREVER);
            if (p_okCmdName != null && p_okCmdName.length() > 0)
            {
                _alert.addCommand(new Command(p_okCmdName, Command.OK, 0));
            }
            if (p_cancelCmdName != null && p_cancelCmdName.length() > 0)
            {
                _alert.addCommand(new Command(p_cancelCmdName, Command.CANCEL, 0));
            }
            _alert.setCommandListener(new CommandListener()
            {

                public void commandAction(Command c, Displayable d)
                {
                    int cmdType = c.getCommandType();

                    boolean closeAlert = false;
                    if (cmdType == Command.OK)
                    {
                        closeAlert = p_alertCommandListener.onOK();
                    }
                    else if (cmdType == Command.CANCEL)
                    {
                        closeAlert = p_alertCommandListener.onCancel();
                    }

                    if (closeAlert == true)
                    {
                        _display.setCurrent(_currentScreen);
                    }
                }
            });
        }
        else
        {
            _alert.setTimeout(5000);
        }


        if (_display.getCurrent() instanceof Alert)
        {
            _display.setCurrent(_currentScreen);
        }
        try
        {
            Thread.sleep(500);
        }
        catch (Exception ex)
        {
        }

        _display.setCurrent(_alert);
    }

    private static void setLoadingAlert(String p_title, String p_alertText, AlertType p_alertType, int p_loadingScreenInterval)
    {
        // rebuild new Alert obj
        _loadingAlert = new Alert(p_title, p_alertText, null, null);
        _loadingAlert.setTimeout(Alert.FOREVER);

        final Gauge g = new Gauge(null, false, 10, 0);
        //new Gauge("null", false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
        g.setValue(0);


        _loadingAlert.setIndicator(g);
        _loadingAlert.addCommand(new Command(" ", Command.CANCEL, 0)); // for hiding disimiss command
        _loadingScreenRunning = true;
        _loadingScreenInterval = p_loadingScreenInterval;

        _display.setCurrent(_loadingAlert, _currentScreen);
        new Thread(new Runnable()
        {

            public void run()
            {
                while (_loadingScreenRunning == true)
                {
                    int value = (g.getValue() + 1) % g.getMaxValue();
                    g.setValue(value);
                    try
                    {
                        Thread.sleep(_loadingScreenInterval);
                    }
                    catch (Exception ex)
                    {
                    }

                }
            }
        }).start();
    }

    private static void closeLoadingAlert(boolean p_returnToScreen)
    {
        _loadingScreenRunning = false;
        try
        {
            Thread.sleep(_loadingScreenInterval);
        }
        catch (Exception ex)
        {
        }

        if (p_returnToScreen == true)
        {
            System.out.println("closeLoadingAlert | " + _currentScreen.toString());
            _display.setCurrent(_currentScreen);
        }

    }

    public static void runBackgroundOperation(String p_alertTitle, String p_alertText, final IBackgroundOperation p_iBgOp)
    {
        setLoadingAlert(p_alertTitle, p_alertText, AlertType.INFO, 1000);

        new Thread(new Runnable()
        {

            public void run()
            {
                boolean returnToScreen = true;
                if (p_iBgOp != null)
                {
                    returnToScreen = p_iBgOp.execute();
                }

                closeLoadingAlert(returnToScreen);
            }
        }).start();
    }

    public static void goToScreen(int p_screen, Object p_data)
    {
        Displayable displayable = null;
        switch (p_screen)
        {
            case Screens.Login:
                displayable = new FormLogin();
                break;

            case Screens.MainMenu:
                displayable = new FormMainMenu(p_data);
                break;

            case Screens.MyGames:
                displayable = new FormMyGames(p_data);
                break;

            case Screens.MyGameInfo:
                displayable = new FormMyGameInfo(p_data);
                break;

            case Screens.NewGame1:
                displayable = new FormNewGame1(p_data);
                break;

            case Screens.NewGame2:
                displayable = new FormNewGame2(p_data);
                break;

            case Screens.About:
                displayable = new FormAbout();
                break;
        }

        _display.setCurrent(displayable);
        _currentScreen = displayable;

        // garbage collector for old data...
        System.gc();
    }
}
